#include <stdio.h>
#include <sys/time.h>

#include "time_code.h"

int set_time_stamp(TIME_STAMP * time_stamp){

	unsigned long long time_sec;
    struct timeval time;

    gettimeofday(&time, NULL);
	time_stamp->second = (unsigned long long )time.tv_sec;
	time_stamp->usecond = (unsigned int)time.tv_usec;

	return 0;    
}

unsigned long long get_time_interval_msec(TIME_STAMP *time_stamp){

	unsigned long long time_interval_msec, pre_sec, now_sec, pre_msec, now_msec, msec;
	struct timeval time_stamp_now;

    gettimeofday(&time_stamp_now, NULL);

	if (time_stamp->second > (unsigned long long )time_stamp_now.tv_sec) {
		printf("current time behind timestamp\n");
	}

	pre_sec = time_stamp->second;
	now_sec = time_stamp_now.tv_sec;
	
	pre_msec = time_stamp->usecond/1000;
	now_msec = time_stamp_now.tv_usec/1000;

	msec = (now_sec - pre_sec)*1000 + (now_msec-pre_msec);

	return msec;	
}

unsigned long long get_time_interval_usec(TIME_STAMP *time_stamp){

	unsigned long long pre_sec, now_sec, pre_usec, now_usec, usec;
	struct timeval time_stamp_now;

    gettimeofday(&time_stamp_now, NULL);

	if (time_stamp->second > (unsigned long long )time_stamp_now.tv_sec) {
		printf("current time behind timestamp\n");
	}

	pre_sec = time_stamp->second;
	now_sec = time_stamp_now.tv_sec;
	
	pre_usec = time_stamp->usecond;
	now_usec = time_stamp_now.tv_usec;

	usec = (now_sec - pre_sec)*1000*1000 + (now_usec-pre_usec);

	return usec;	
}

